from  application import *

class AbstractCellPickerAgent(AbstractAgent):
    def __init__(self):
        AbstractAgent.__init__(self,AgentType.exclusive)
    def ProcesEvent(self, event, agentmanager):        
        if event.type==MOUSEBUTTONDOWN and event.mouse.button==1 or event.type==MOUSEMOTION and event.mouse.buttons[0]==1:            
            cell=0;map=0
            for e in event.pick:            
                if e.model.__class__.__name__ == 'CellM': cell=e.model
                elif e.model.__class__.__name__ == 'MapM': map=e.model
                if map and cell:                    
                    return self.OnCellEvent(cell,map,event)                    
        return 0
    def OnCellEvent(self, cell, map, event):
        return 0

class EraserA(AbstractCellPickerAgent):
    def __init__(self):
        self.mode=0
        AbstractCellPickerAgent.__init__(self)  
    def OnCellEvent(self, cell, map, event):
        if event.type==MOUSEBUTTONDOWN:             
            self.mode=0            
        if self.mode == 0 or self.mode == 1:
            for e in event.pick:
                if e.model in  map.objects:
                    self.mode=1
                    map.controler.RemoveObject(e.model)
                    break                    
        if self.mode == 0 or self.mode == 2:
            cell.controler.SetPrototype(0)
            self.mode=2        
        return 1
        
class PaintTemplateObjectA(AbstractCellPickerAgent):
    def __init__(self,  prototype):
        AbstractCellPickerAgent.__init__(self)
        self.prototype=prototype
    def OnCellEvent(self, cell, map, event):
        for o in map.objects:
            if o.prototype==self.prototype and o.coord==cell.coord:
                return 0  
        map.controler.AddObjectProto(cell.coord,self.prototype)                        
        return 1
        
class PaintTemplateCellA(AbstractCellPickerAgent):
    def __init__(self,  prototype):
        AbstractAgent.__init__(self,AgentType.exclusive)
        self.prototype=prototype
    def OnCellEvent(self, cell, map, event): 
        if cell.prototype!=self.prototype:
           cell.controler.SetPrototype(self.prototype)                    
        return 1